package com.mmy.acassistant.model.main.ui.fragment

import com.mmy.acassistant.R
import com.mmy.acassistant.model.main.component.DaggerHomeComponent
import com.mmy.acassistant.model.main.module.HomeModule
import com.mmy.acassistant.model.main.presenter.HomePresenter
import com.mmy.acassistant.model.main.view.HomeView
import com.mmy.frame.AppComponent
import com.mmy.frame.base.view.BaseFragment
import kotlinx.android.synthetic.main.fragment_home.*
import java.net.ServerSocket
import kotlin.concurrent.thread


/**
 * @创建者     lucas
 * @创建时间   2017/12/27 0027 14:14
 * @描述          主页
 */
class HomeFragment : BaseFragment<HomePresenter>(), HomeView {
    override fun setupDagger(appComponent: AppComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(HomeModule(this))
                .build().inject(this)
    }

    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initView() {
        //模拟socket服务器端
        thread {
            Thread.currentThread().name.ld()
            val serverSocket = ServerSocket(5556)
            //等待链接
            val socket = serverSocket.accept()
            val inputStream = socket.getInputStream()
            var buffer = ByteArray(4 * 1024)
            var leng = inputStream.read(buffer)
            var stringBuider = StringBuilder()
            if (leng != -1) {
                stringBuider.append(String(buffer,0,leng))
                leng = inputStream.read(buffer)
            }
            inputStream.close()
            activity.runOnUiThread {
                text.text = stringBuider.toString()
            }
        }
    }

    override fun initData() {
        mIPresenter.loadData()
    }

    override fun initEvent() {
    }
}